<template>
	<view class="comments">
		<view class="commentList">
			<view class="comNum" v-if="this.comNum!=0">全部评论({{this.comNum}}) ：</view>
			<view v-for="item in comList" :key="item.id" class="comList">
				<text class="name">{{item.userName}}：</text>{{item.content}}
				<view class="time">{{item.commentTime}}</view>
			</view>
		</view>
		
		<view class="comment">
			<input type="text" placeholder="发表一条精彩的评论" v-model="com">
			<button size="mini" :disabled="com!=''? false:true" @click="sendCom">发表</button>
		</view>
		
		<!-- 有高度的空内容，防止评论框遮挡 -->
		<view class="no"></view>
	</view>
</template>

<script>
	import { http } from "../../utils/request.js"
	export default {
		data() {
			return {
				id:0,
				comNum:0,
				comList:[],
				com:""
			}
		},
		onLoad(e) {
			this.id=e.id
			this.getComnum()
			this.getComlist()
			// 将标题改为对应活动详情的标题
			uni.setNavigationBarTitle({
				title:e.title
			})
		},
		methods: {
			// 获取活动评论数
			getComnum(){
				http({
					url:"/prod-api/api/activity/comment/number",
					data:{
						activityId:this.id
					}
				}).then(res=>{
					if(res.data.code==200){
						this.comNum=res.data.commentNum
					}else{
						uni.showToast({
							title: res.data.msg,
							icon:"none"
						})
					}
				})
			},
			// 获取活动评论列表
			getComlist(){
				http({
					url:"/prod-api/api/activity/comment/list",
					data:{
						activityId:this.id
					}
				}).then(res=>{
					if(res.data.code==200){
						this.comList=res.data.rows
					}else{
						uni.showToast({
							title: res.data.msg,
							icon:"none"
						})
					}
				})
			},
			// 发表评论
			sendCom(){
				http({
					url:"/prod-api/api/activity/comment",
					method:"POST",
					data:{
						activityId:this.id,
						content:this.com
					}
				}).then(res=>{
					if(res.data.code==200){
						uni.showToast({
							title:"发表成功！"
						})
						this.com=""
						this.getComnum()
						this.getComlist()
					}else{
						uni.showToast({
							title:res.data.msg,
							icon:"none"
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
.comments{
	.commentList{
		padding: 20rpx;
		.comNum{
			padding: 20rpx;
			font-weight: bold;
			border-radius: 20rpx;
			background-color: #ccc;
		}
		.comList{
			padding: 10rpx 20rpx;
			border-radius: 20rpx;
			background-color: #f8f8f8;
			margin: 10rpx 0;
			.name{
				font-weight: bold;
			}
			.time{
				text-align: right;
				color: #ccc;
				font-size: 26rpx;
			}
		}
		.comList:last-child{
			margin-bottom: 0;
		}
	}
	.comment{
		padding: 20rpx;
		border-top: 1px solid #ccc;
		display: flex;
		justify-content: space-between;
		align-items: center;
		background-color: #fff;
		
		position: fixed;
		bottom: 0;
		width: 100%;
		height: 120rpx;
		box-sizing: border-box;
		input{
			flex: 1;
			background-color: #f8f8f8;
			border: 1px solid #ccc;
			padding: 10rpx 20rpx;
			border-radius: 15rpx;
			box-sizing: border-box;
			height:80rpx;
			font-size: 30rpx;
		}
		button{
			height: 80rpx;
			line-height: 80rpx;
			font-size: 30rpx;
			margin: 0;
			margin-left: 20rpx;
		}
	}
	.no{
		height: 120rpx;
	}
}
</style>
